package com.imageim.yimirror.common.ui.drawbody;

import android.graphics.Path;
import android.provider.ContactsContract;

import com.imageim.yimirror.common.util.LogUtil;

import java.util.List;

/**
 * Created by Bruce.Zhou on 2018/9/17 15:08.
 * Email: 907160968@qq.com
 */

public class BodyLine {
    private List<BodyPoint> pointList;
    private int type; // 1实线， 0虚线
    public float moveDeltaX;
    public float moveDeltaY;
    public float endX; // 连线终点
    public float endY;
    public float indicatorEndX;
    public float indicatorEndY;
    private float moveSpeed = 8;
    public Path path;
    public static final int TYPE_FULL = 1;
    public static final int TYPE_DOTTED = 0;
    private int alpha = 80;
    private int finishDistance = 4;

    public BodyLine() {
        type = TYPE_FULL;
        path = new Path();
    }

    public List<BodyPoint> getPointList() {
        return pointList;
    }

    public void setPointList(List<BodyPoint> pointList) {
        this.pointList = pointList;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getAlpha() {
        return alpha;
    }

    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

    public boolean isFinished() {
        if (pointList == null) {
            return false;
        }
        for (BodyPoint point : pointList) {
            if (point.getStatus() != PointStatus.STATUS_FINISHED) {
                return false;
            }
        }
        return true;
    }

    public int getCurrStartedPointIndex() {
        for (int i = 0; i < pointList.size(); i++) {
            BodyPoint point = pointList.get(i);
            if (point.getStatus() == PointStatus.STATUS_STARTED) {
                return i;
            }
        }
        return -1;
    }

    private int getCurrIndicatorStartedPointIndex() {
        for (int i = 0; i < pointList.size(); i++) {
            BodyPoint point = pointList.get(i);
            if (point.getIndicatorStatus() == IndicatorStatus.STATUS_STARTED) {
                return i;
            }
        }
        return -1;
    }

    public void setMoveDelta() {
        int index = getCurrStartedPointIndex();
        if (index == -1 || index == pointList.size() - 1) {
            return;
        }
        BodyPoint startPoint = pointList.get(index);
        BodyPoint endPoint = pointList.get(index + 1);
        float distance = (float) Math.sqrt( Math.pow((endPoint.getY() - startPoint.getY()), 2)
                + Math.pow((endPoint.getX() - startPoint.getX()), 2) );
        float rate = moveSpeed / distance;

        moveDeltaX = (endPoint.getX() - startPoint.getX()) * rate;
        moveDeltaY = (endPoint.getY() - startPoint.getY()) * rate;
        if (type == TYPE_DOTTED) {
            moveDeltaX *= 1.5f;
            moveDeltaY *= 1.5f;
        }
//        LogUtil.d("body", "moveDeltaX=" + moveDeltaX + ", moveDeltaY=" + moveDeltaY);
    }

    public void setIndicatorMoveDelta() {
        int index = getCurrIndicatorStartedPointIndex();
        if (index == -1 || index == pointList.size() - 1) {
            return;
        }
        BodyPoint startPoint = pointList.get(index);
        BodyPoint endPoint = pointList.get(index + 1);
        float distance = (float) Math.sqrt( Math.pow((endPoint.getY() - startPoint.getY()), 2)
                + Math.pow((endPoint.getX() - startPoint.getX()), 2) );
        float rate = moveSpeed / distance;

        moveDeltaX = (endPoint.getX() - startPoint.getX()) * rate;
        moveDeltaY = (endPoint.getY() - startPoint.getY()) * rate;
    }

    public void udpateBodyPointStatus() {
        int currStartedIndex = getCurrStartedPointIndex();
        if (currStartedIndex == -1) {
            return;
        }
        if (currStartedIndex == pointList.size() - 1) { // 最后一个点
            pointList.get(currStartedIndex).setStatus(PointStatus.STATUS_FINISHED);
            return;
        }
        BodyPoint currStartedPoint = pointList.get(currStartedIndex);
        BodyPoint nextUnstartedPoint = pointList.get(currStartedIndex + 1);
        // 移动点距离下一个起点的距离
        float distance = (float) Math.sqrt(Math.pow(nextUnstartedPoint.getX() - endX, 2)
                + Math.pow(nextUnstartedPoint.getY() - endY, 2));
        if (type == TYPE_FULL) {
            finishDistance = 4;
        } else {
            finishDistance = 6;
        }
        if (distance < finishDistance) {
            currStartedPoint.setStatus(PointStatus.STATUS_FINISHED);
            nextUnstartedPoint.setStatus(PointStatus.STATUS_STARTED);
            endX = nextUnstartedPoint.getX();
            endY = nextUnstartedPoint.getY();
            setMoveDelta();
        }
    }

    public void udpateBodyPointIndicatorStatus() {
        int currStartedIndex = getCurrIndicatorStartedPointIndex();
        if (currStartedIndex == -1) {
            return;
        }
        if (currStartedIndex == pointList.size() - 1) { // 最后一个点
//            pointList.get(currStartedIndex).setStatus(PointStatus.STATUS_FINISHED);
            resetIndicatorStatus();
            return;
        }
        BodyPoint currStartedPoint = pointList.get(currStartedIndex);
        BodyPoint nextUnstartedPoint = pointList.get(currStartedIndex + 1);
        // 移动点距离下一个起点的距离
        float distance = (float) Math.sqrt(Math.pow(nextUnstartedPoint.getX() - indicatorEndX, 2)
                + Math.pow(nextUnstartedPoint.getY() - indicatorEndY, 2));
        if (distance < 8) {
            currStartedPoint.setIndicatorStatus(IndicatorStatus.STATUS_FINISHED);
            nextUnstartedPoint.setIndicatorStatus(IndicatorStatus.STATUS_STARTED);
            indicatorEndX = nextUnstartedPoint.getX();
            indicatorEndY = nextUnstartedPoint.getY();
            setIndicatorMoveDelta();
        }
    }

    private void resetIndicatorStatus() {
        for (int i = 0; i < pointList.size(); i++) {
            if (i == 0) {
                pointList.get(i).setIndicatorStatus(IndicatorStatus.STATUS_STARTED);
            } else {
                pointList.get(i).setIndicatorStatus(IndicatorStatus.STATUS_UNSTART);
            }
        }
        indicatorEndX = pointList.get(0).getX();
        indicatorEndY = pointList.get(0).getY();
    }
}
